package com.ys.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.entity.business.TpAplusFile;
import com.ys.entity.business.TpBoutiqueSiteFile;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.business.TpAplusFileMapper;
import com.ys.mapper.business.TpBoutiqueSiteFileMapper;
import com.ys.service.business.TpBoutiqueSiteFileService;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Map;


/**
 * 精品库附件
 */
@Service("tpBoutiqueSiteFileService")
public class TpBoutiqueSiteFileServiceImpl implements TpBoutiqueSiteFileService {
	private static final Logger logger = LoggerFactory.getLogger(TpBoutiqueSiteFileServiceImpl.class);

	@Autowired
	private TpBoutiqueSiteFileMapper tpBoutiqueSiteFileMapper;

	@Autowired
	private DiySqlMapper diySqlMapper;



	/**
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params) throws Exception{
		logger.info("精品库附件新增传参："+params);
		TpBoutiqueSiteFile tpBoutiqueSiteFile = JSON.parseObject(JSON.toJSONString(params), TpBoutiqueSiteFile.class);
		int res = tpBoutiqueSiteFileMapper.insert(tpBoutiqueSiteFile);
		if(res>0){
			return R.ok();
		}else {
			return R.error("操作失败！");
		}
	}



	/**
	 * 修改
	 */
	@Override
	public R update(Map<String, Object> params) {
		logger.info("精品库附件修改传参："+params);
		TpBoutiqueSiteFile tpBoutiqueSiteFile = JSON.parseObject(JSON.toJSONString(params), TpBoutiqueSiteFile.class);
		int res = tpBoutiqueSiteFileMapper.update(tpBoutiqueSiteFile);
		if(res>0){
			return R.ok();
		}else {
			return R.error("操作失败！");
		}
	}

	//批量新增、修改
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R addOrUpdateByList(Map<String, Object> params) throws Exception {
		JSONArray dataList = JSONArray.parseArray(params.get("dataList").toString());
		//int res = 0;
		if(dataList.size()>0) {
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject jsonObj = dataList.getJSONObject(i);
				TpBoutiqueSiteFile tpBoutiqueSiteFile = new TpBoutiqueSiteFile();
				if(Long.parseLong(jsonObj.get("id").toString())<=0){//新增
					tpBoutiqueSiteFile.setBoutiqueId(Long.parseLong(params.get("boutiqueId").toString()));//精品库Id
					tpBoutiqueSiteFile.setBoutiqueSiteDetailId(Long.parseLong(params.get("boutiqueSiteDetailId").toString()));//精品库站点信息表Id
					tpBoutiqueSiteFile.setFileName(jsonObj.get("fileName").toString());//附件名称
					tpBoutiqueSiteFile.setFilePath(jsonObj.get("filePath").toString());//附件链接
					tpBoutiqueSiteFile.setFileType(jsonObj.get("fileType").toString());//附件文件类型（后缀）
					tpBoutiqueSiteFile.setFileSize(jsonObj.get("fileSize").toString());//附件大小
					tpBoutiqueSiteFile.setFileSizeUnit(jsonObj.get("fileSizeUnit").toString());//附件大小单位
					tpBoutiqueSiteFile.setStatus(1);
					tpBoutiqueSiteFileMapper.insert(tpBoutiqueSiteFile);
				}else {//修改
					tpBoutiqueSiteFile.setId(Long.parseLong(jsonObj.get("id").toString()));//id
					tpBoutiqueSiteFile.setBoutiqueId(Long.parseLong(params.get("boutiqueId").toString()));//精品库Id
					tpBoutiqueSiteFile.setBoutiqueSiteDetailId(Long.parseLong(params.get("boutiqueSiteDetailId").toString()));//精品库站点信息表Id
					tpBoutiqueSiteFile.setFileName(jsonObj.get("fileName").toString());//附件名称
					tpBoutiqueSiteFile.setFilePath(jsonObj.get("filePath").toString());//附件链接
					tpBoutiqueSiteFile.setFileType(jsonObj.get("fileType").toString());//附件文件类型（后缀）
					tpBoutiqueSiteFile.setFileSize(jsonObj.get("fileSize").toString());//附件大小
					tpBoutiqueSiteFile.setFileSizeUnit(jsonObj.get("fileSizeUnit").toString());//附件大小单位
					tpBoutiqueSiteFile.setStatus(1);
					tpBoutiqueSiteFileMapper.update(tpBoutiqueSiteFile);
				}
			}
		}
		return R.ok();
	}


	//删除
	@Override
	public void del(long id){
		 tpBoutiqueSiteFileMapper.deleteTrue(id);
	}

	//根据精品库站点信息表Id删除
	@Override
	public int delByBoutiqueSiteDetailId(long id){
		diySqlMapper.delBoutiqueSiteFileByBoutiqueSiteDetailId(id);
		return 1;
	}
}
